﻿Public Class SelectDepartingFlight
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        btnSeat.PostBackUrl = "SelectSeat.aspx"
    End Sub

    Protected Sub btnContinue_Click(sender As Object, e As EventArgs) Handles btnContinue.Click
        If (txtUserId.Text <> "" And txtFirstName.Text <> "" And txtLastName.Text <> "") Then

            Dim objConn As New MySql.Data.MySqlClient.MySqlConnection
            Dim objCmd As New MySql.Data.MySqlClient.MySqlCommand
            Dim dtAdapter As New MySql.Data.MySqlClient.MySqlDataAdapter

            Dim strConnString
            Dim username As String

            'Creates username first char of first name + last name
            username = txtFirstName.Text(0) + txtLastName.Text

            strConnString = "Server=localhost;User Id=goldair_dapp; Password=geckodev; Database=goldair_db; Pooling=false"

            objConn.ConnectionString = strConnString
            objConn.Open()

            'SAVES USER INFORMATION
            With objCmd
                .Connection = objConn
                .CommandText = "Insert into user values(" & txtUserId.Text & ",'" & txtFirstName.Text & "','" & txtLastName.Text & "','" & txtEmail.Text & "','" & username & "','none',2,0,'0001-01-01 00:00:00')"
                .CommandType = CommandType.Text

            End With
            Try
                objCmd.ExecuteNonQuery()

            Catch ex As Exception
                If ex.Message.ToString.Contains("Duplicate") Then
                    MsgBox("User Already Exist, Information Updated", vbInformation)

                    With objCmd
                        .Connection = objConn
                        .CommandText = "Update user set first_name='" & txtFirstName.Text & "', last_name='" & txtLastName.Text & "',email='" & txtEmail.Text & "',user_name='" & username & "' where id=" & txtUserId.Text & " "
                        .CommandType = CommandType.Text
                    End With

                    objCmd.ExecuteNonQuery()
                Else
                    MsgBox("Check the Data", vbCritical)
                End If

            End Try

            'SAVES THE USER FLIGHT INFORMATION
            With objCmd
                .Connection = objConn
                .CommandText = "Insert into user_flight values(" & txtUserId.Text & "," & txtFlightNum.Text & ",0,0,'0001-01-01 00:00:00',0,0,'0001-01-01 00:00:00')"
                .CommandType = CommandType.Text

            End With
            Try
                objCmd.ExecuteNonQuery()

            Catch ex As Exception
                MsgBox("Please check the Data", vbCritical)
            End Try

            objConn.Close()
            MsgBox("Information Saved, Please Continue", vbInformation)
            '  Response.Redirect("Summary.aspx")
        Else
            MsgBox("Please Fill all the Data!", vbCritical)

        End If


    End Sub

    
End Class